# -*- coding: utf-8 -*-
# @Time : 2021/6/25 14:56

# @Author : zhujianhao

# @File : personal-code-link.py

# @Software: PyCharm

from locust import HttpLocust, TaskSet, task
import random
from faker import Faker
from locust import HttpUser, task, between, HttpLocust
from locust import LoadTestShape
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
fake = Faker(locale='zh_CN')
import json


class QuickstartUser(TaskSet):
    def on_start(self):
        pass

    @task
    def api1(self):
        headers = {
            "Host": "market-uat.tangees.com",
            "Connection": "keep-alive",
            "Content-Length": "482",
            "Accept": "*/*",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63",
            "Content-Type": "application/json;charset=UTF-8",
            "Origin": "https://market-uat.tangees.com",
            "Sec-Fetch-Site": "same-origin",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Dest": "empty",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
        }
        payload = {
            "phone": "18581813000",
            "name": "张三01",
            "desc": "接口测试",
            "wechatId": "18581813523",
        }

        with self.client.post(url='/common-api/scrm/open/thirdClue/save/1423227274639048705', headers=headers, data=json.dumps(payload),
                              catch_response=True) as response:

            # {"traceId":"AF6CCD0495D54A87A223FA90454D62C3","errCode":0,"errMsg":"success！","data":null}
            if response.status_code == 200:
                try:
                    json.loads(response.text)['errMsg'] == 'success！'
                except:
                    response.failure("Timeout")
            else:
                response.failure(response.status_code)

    def on_stop(self):
        pass


class WebsiteUser(HttpUser):
    tasks = [QuickstartUser]
    host = "https://common-uat.tangees.com"
    wait_time = between(0.1, 0.5)  # 多少秒用户发起1次请求


if __name__ == "__main__":
    import os

    os.system("locust -f locust_三方线索接入.py --web-host=127.0.0.1")
